﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace BW.Mes.WinForms.WareHouse
{
    public partial class CreateWareHouse : Form
    {
        private readonly HttpClient _httpClient;

        

        public CreateWareHouse()
        {
            InitializeComponent();
            // 初始化HttpClient
            _httpClient = new HttpClient();
        }



        // 仓库类型模型
        public class MoIdModel
        {
            public long Id { get; set; }
            public string MoIdName { get; set; }
        }



        /// <summary>
        /// 仓库定义的实体
        /// </summary>
        public class WareHouseModel
        {
            /// <summary>
            /// 仓库定义编码
            /// </summary>
            public string WareHouseCoding { get; set; }
            /// <summary>
            /// 仓库定义名称
            /// </summary>
            public string WareHouseName { get; set; }
            /// <summary>
            /// 仓库定义类型 外键
            /// </summary>
            public long WareHouseStatus { get; set; }
            /// <summary>
            /// 仓库定音仓管员
            /// </summary>
            public long Loginname { get; set; }
            /// <summary>
            /// 仓库定义地址
            /// </summary>
            public string WareHouseAddress { get; set; }
            /// <summary>
            /// 仓库定义描述
            /// </summary>
            public string WareHouseDescribe { get; set; }
        }



        /// <summary>
        /// 点击取消关闭弹窗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 添加到后端api方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnSave_Click(object sender, EventArgs e)
        {
            
                // 从窗体控件获取数据并赋值给模型
                var product = new WareHouseModel
                {
                    WareHouseCoding = textbm.Text,
                    WareHouseName = textName.Text,
                    WareHouseStatus = comboBoxEdit1.EditValue != null ? Convert.ToInt64(comboBoxEdit1.EditValue) : 0,
                    Loginname = comboBoxEdit2.EditValue != null ? Convert.ToInt64(comboBoxEdit2.EditValue) : 0,
                    WareHouseAddress = textBoxAddress.Text,
                    WareHouseDescribe = textBoxMs.Text,
                };

                // 调用 API 方法
                using (var client = new HttpClient())
                {
                    //设置请求地址
                    client.BaseAddress=new Uri("https://localhost:7212");
                    //创建JSON数据（转换数据类型）
                    var json = JsonConvert.SerializeObject(product);
                    //创建请求内容
                    var  content =new StringContent(json, Encoding.UTF8, "application/json");
                    //发送POST请求
                    var response = await client.PostAsync("api/WareHouse/CreateWareHouse", content);
                    //判断是否成功
                    if (response.IsSuccessStatusCode)
                    {
                        XtraMessageBox.Show("保存成功");
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        XtraMessageBox.Show("保存失败:"+response.ReasonPhrase);
                    }
                }
                textbm.Clear();
                textName.Clear();
                textBoxAddress.Clear();
                textBoxMs.Clear();
            
        }




        private void simpleButton2_Click(object sender, EventArgs e)
        {
            btnSave_Click(sender,e);
        }

        private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
